﻿/*
 * NOT DONE
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using DTO;
using DA;

namespace GUI.View
{
    public partial class TimKiemNangCao : UserControl
    {
        private MyQueryDA myQueryDA;
        private DiaOcBLL diaocDA;
        private LoaiDiaOcBLL loaidiaocDA;
        private String _tuNgay = "";
        private String _denNgay = "";

        private AppCommands _Commands;
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                btnDonGian.Command = newValue.TimKiemNangCaoCommands.Cancel;
            }
            else
            {
                btnDonGian.Command = null;
            }
        }

        public TimKiemNangCao()
        {
            InitializeComponent();
            myQueryDA = new MyQueryDA();
            diaocDA = new DiaOcBLL();
            loaidiaocDA = new LoaiDiaOcBLL();
            LoaiDiaOc();
        }
        public void LoaiDiaOc()
        {
            cbbLoaiDiaOc.DataSource = loaidiaocDA.GetAll().Tables[0];
            cbbLoaiDiaOc.DisplayMember = "TenLoaiDiaOc";
            cbbLoaiDiaOc.ValueMember = "MaLoaiDiaOc";
        }

        public string GetDateTime(DateTime dt)
        {
            return String.Format("{0}/{1}/{2}", dt.Year, dt.Month, dt.Day);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sqlMaKhachHang;
            if (!txtMaKhachHang.Text.Equals(""))
            {
                sqlMaKhachHang = String.Format("and KHACHHANG.MaKH like '%{0}%'", txtMaKhachHang.Text);
            }
            else
            {
                sqlMaKhachHang = "";
            }
            Console.WriteLine(txtMaKhachHang.Text);

            //Textbox Họ Tên
            string sqlHoTen;
            if (!txtHoTen.Text.Equals(""))
            {
                sqlHoTen = String.Format("and HoTen like N'%{0}%'", txtHoTen.Text);
            }
            else
            {
                sqlHoTen = "";
            }

            //Textbox DienTichDat
            string sqlDienTichDat;
            if (!txtDienTichDat.Text.Equals(""))
            {
                sqlDienTichDat = String.Format("and DienTichDat like '%{0}%'", txtDienTichDat.Text);
            }
            else
            {
                sqlDienTichDat = "";
            }

            //Textbox DienTichXayDung
            string sqlDienTichXayDung;
            if (!txtDienTichXayDung.Text.Equals(""))
            {
                sqlDienTichXayDung = String.Format("and DienTichXayDung like '%{0}%'", txtDienTichXayDung.Text);
            }
            else
            {
                sqlDienTichXayDung = "";
            }

            //TODO txtViTri


            //txtSoDienThoai
            string sqlSoDT;
            if (!txtSoDienThoai.Text.Equals(""))
            {
                sqlSoDT = String.Format("and SoDT like '%{0}%'", txtSoDienThoai.Text);
            }
            else
            {
                sqlSoDT = "";
            }

            //txtDiaChiDO
            string sqlDiaChiDO;
            if (!txtDiaChiDiaOc.Text.Equals(""))
            {
                sqlDiaChiDO = String.Format("and DiaChiDiaOc like N'%{0}%'", txtDiaChiDiaOc.Text);
            }
            else
            {
                sqlDiaChiDO = "";
            }

            //txtEmail
            string sqlEmail;
            if (!txtEmail.Text.Equals(""))
            {
                sqlEmail = String.Format("and Email like '%{0}%'", txtEmail.Text);
            }
            else
            {
                sqlEmail = "";
            }

            //dateStart
            string queryTuNgay;
            if (!dateStart.Text.Equals("") && dateFinish.Text.Equals(""))
            {
                queryTuNgay = String.Format("and TuNgay >= '{0}'", GetDateTime(dateStart.Value));
            }
            else
                if (dateStart.Text.Equals("") && !dateFinish.Text.Equals(""))
                {

                    queryTuNgay = String.Format("and TuNgay <= '{0}'", GetDateTime(dateFinish.Value));
                }
                else
                    if (!dateStart.Text.Equals("") && !dateFinish.Text.Equals(""))
                    {
                        queryTuNgay = String.Format("and TuNgay >= '{0}' and TuNgay <= ' {1}'", GetDateTime(dateStart.Value), GetDateTime(dateFinish.Value));
                    }
                    else
                        queryTuNgay = "";

            //cbbLoaiDiaOc
            string sqlLoaiDiaOc;
            if (cbbLoaiDiaOc.SelectedItem == null)
            {
                sqlLoaiDiaOc = "";
            }
            else
            {
                sqlLoaiDiaOc = String.Format("and TenLoaiDiaOc like N'%{0}%'", cbbLoaiDiaOc.GetItemText(cbbLoaiDiaOc.SelectedItem).Trim());
            }

            //cbbHuong
            string sqlHuong;
            if (cbbHuong.SelectedItem == null)
            {
                sqlHuong = "";
            }
            else
            {
                sqlHuong = String.Format("and Huong = N'{0}'", cbbHuong.GetItemText(cbbHuong.SelectedItem).Trim());
            }
            string madiaoc;
            if (txtMaDiaOc.Text != "")
            {
                madiaoc = String.Format("and DIAOC.MaDiaOc like '%{0}%'", txtMaDiaOc.Text);
            }
            else
            {
                madiaoc = "";
            }

            //Câu lện truy vấn tìm kiếm nâng cao
            String sqlQuery = String.Format("select KHACHHANG.MaKH,HoTen,SoDT,Email,DiaChi,DIAOC.MaDiaOc,DiaChiDiaOc,ViTri from  KHACHHANG inner join DIAOC on KHACHHANG.MaKH=DIAOC.MaKH inner join LOAIDIAOC on DIAOC.MaLoaiDiaOc=LOAIDIAOC.MaLoaiDiaOc inner join PHIEUDANGKY on DIAOC.MaDiaOc=PHIEUDANGKY.MaDiaOC {0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}", sqlMaKhachHang, sqlHoTen, sqlDienTichDat, sqlDienTichXayDung, sqlSoDT, queryTuNgay, madiaoc, sqlDiaChiDO, sqlHuong, sqlEmail, sqlLoaiDiaOc);// +  " " + sqlLoaiDiaOc+ " " + sqlDiaChiDO + " " + sqlHuong + " " + sqlEmail;

            if (myQueryDA.doQuery(sqlQuery) == null)
            {
                MessageBox.Show(" Không Tìm Thấy");
                return;
            }

            DataTable dt = myQueryDA.doQuery(sqlQuery).Tables[0];
            dataGridViewTimKiemKhachHang.DataSource = dt;

            for (int i = 0; i < dataGridViewTimKiemKhachHang.Rows.Count - 1; i++)
            {
                dataGridViewTimKiemKhachHang.Rows[i].Cells[0].Value = i + 1;
            }

            if (dt.Rows.Count == 0)
            {
                ToastNotification.Show(this, "Không tìm thấy khách hàng.", eToastPosition.MiddleCenter);
            }
            btnChiTiet.Enabled = false;
            dataGridViewTimKiemKhachHang.Refresh();
        }

        private void dateStart_ValueChanged(object sender, EventArgs e)
        {
            _tuNgay = String.Format("{0}/{1}/{2}", dateStart.Value.Year, dateStart.Value.Month, dateStart.Value.Day);
        }

        private void dateFinish_ValueChanged(object sender, EventArgs e)
        {
            _denNgay = String.Format("{0}/{1}/{2}", dateFinish.Value.Year, dateFinish.Value.Month, dateFinish.Value.Day);
        }

        #region điều kiện nhập
        private void txtMaKhachHang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsLetter(e.KeyChar) && (e.KeyChar != 8 || e.KeyChar != 13))
                e.Handled = true;
            if (e.KeyChar == 8)
                e.Handled = false;
            if (e.KeyChar == (char)Keys.Space)
                e.Handled = false;
            if (e.KeyChar == 39)
                e.Handled = false;
        }

        private void txtMaDiaOc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDienTichDat_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDienTichXayDung_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        #endregion

        private void dataGridViewTimKiemKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.RowIndex;

            try
            {
                if (i > -1 && i < dataGridViewTimKiemKhachHang.RowCount - 1)
                {
                    btnChiTiet.Enabled = true;
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            try
            {
                int currenColumn = dataGridViewTimKiemKhachHang.CurrentCell.RowIndex;
                ChiTietKhachHangControl.makhachhang = Convert.ToInt32(dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["MaKH"].Value);
                ChiTietKhachHangControl.hoten = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["HoTen"].Value.ToString();
                ChiTietKhachHangControl.sodienthoai = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["SoDT"].Value.ToString();
                ChiTietKhachHangControl.email = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["Email"].Value.ToString();
                ChiTietKhachHangControl.diachi = dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["DiaChi"].Value.ToString();
                ChiTietKhachHangControl._madiaoc = Convert.ToInt32(dataGridViewTimKiemKhachHang.Rows[currenColumn].Cells["MaDiaOc"].Value);
                //HACK btnChiTiet.Command = _Commands.ChiTietKhachHangCommands.New;
            }
            catch
            {
                MessageBox.Show("Chưa Chọn Bệnh Nhân", "Chi Tiết Bệnh Nhân", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            
        }

    }
}
